package com.baosight.sczx.zm.cm.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.Utils;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cc.Qualitystandard;
import com.baosight.sczx.common.zm.cInterface.cl.RiskRecord;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBasicInfo;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfo;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfoReport;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialType;



/**材料使用管理--材料基础信息
 * <p>Title: ServiceZMCMK1.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 杨鸣
 * @version 1.0
 * <pre>Histroy:
 *           2012-03-07下午15:31:29  高强
 *</pre>
 */

public class ServiceZMCMK1 extends ServiceEPBase {
	String treeTypeInnerCode ="";
	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-03-07下午15:31:29  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units",false));
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-03-07下午15:31:29  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		treeTypeInnerCode = inInfo.getString("treeTypeInnerCode");//树节点ID
		if(treeTypeInnerCode != null){
			StaticTools.setQueryParameters(inInfo, "treeTypeInnerCode", treeTypeInnerCode, true);//添加查询条件
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfoReport.query",new ZMCMMaterialBasicInfoReport());
		return outInfo;
	}
	
	public EiInfo queryMaterial(EiInfo inInfo)
	{
		EiInfo outInfo = MaterialBasicInfo.getMaterialInfo(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units",false));
		return outInfo;
	}
	
	public EiInfo queryMaterialType(EiInfo inInfo)
    {
    	EiInfo outInfo = new EiInfo();
		outInfo.addBlock(Utils.generateOutBlock(EiConstant.resultBlock, new ZMCMMaterialType(), inInfo, "ZMCMMaterialType.queryForWholeQuery", false));	
		
    	return outInfo;
    }

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:07:00 杨鸣
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiBlock resultBlock = inInfo.getBlock(EiConstant.resultBlock);
		String fdMaterialTypeId = inInfo.getString("fdMaterialTypeId");//主表ID
		String fdMaterialType = inInfo.getString("fdMaterialType");//主表ID
		String fdMaterialTypeInnerCode = inInfo.getString("fdMaterialInnerCode");//主表IDinInfo.getBlock("result").getCellStr(0, "fdMaterialTypeInnerCode");
		String fdMaterialCodes = "";
		
		for(int i = 0; i < resultBlock.getRowCount(); i++)
		{
			String fdMaterialCode = inInfo.getCellStr("result", i, "fdMaterialCode");
			
			resultBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_MATERIALBASICINFO_REPORT"));
			resultBlock.setCell(i, "fdMaterialTypeId", fdMaterialTypeId);//隐患记录ID
			resultBlock.setCell(i, "fdMaterialType", fdMaterialType);
			resultBlock.setCell(i, "fdMaterialInnerCode", fdMaterialTypeInnerCode);
			resultBlock.setCell(i, "fdMaterialName", inInfo.getCellStr("result", i, "fdMaterialName"));
			resultBlock.setCell(i, "fdMaterialCode", inInfo.getCellStr("result", i, "fdMaterialCode"));
			resultBlock.setCell(i, "fdSpecModel", inInfo.getCellStr("result", i, "fdSpecModel"));
			resultBlock.setCell(i, "fdMetric", inInfo.getCellStr("result", i, "fdMetric"));
			
			if(i < (resultBlock.getRowCount() - 1)) {
				fdMaterialCodes += "'"+fdMaterialCode+"',";
			}
			else {
				fdMaterialCodes += "'"+fdMaterialCode+"'";
			}
		}
		
		if(resultBlock.getRowCount() > 0) {
			Map<String, String> args = new HashMap<String, String>();
			args.put("fdMaterialCodes", fdMaterialCodes);
			boolean validate = this.validate(inInfo, args);
			if(validate) {
				return inInfo;
			}
			EiInfo outInfo = super.update(inInfo,"ZMCMMaterialBasicInfoReport.insert");
			List<Map>  _list = new ArrayList();
			Map para = new HashMap();
			EiInfo queryInfo = new EiInfo();
			para.put("treeTypeInnerCode", treeTypeInnerCode);
			_list = dao.query("ZMCMMaterialBasicInfoReport.query", para);
			queryInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCMMaterialBasicInfo().eiMetadata);
			queryInfo.getBlock(EiConstant.resultBlock).addRows(_list);
			EiBlock gridBlock = queryInfo.getBlock("result");
			String materialName ="";
			String metric ="";
	        if (null != gridBlock)
	        { 
	        	int j=1;
	            for (int i = 0; i < gridBlock.getRowCount(); i++)
	            {
	            	if(i!=0)
	            	{
	            		if(gridBlock.getCell(i, "fdMaterialName").equals(materialName) && gridBlock.getCell(i, "fdMetric").equals(metric))
		            	{
		            		gridBlock.setCell(i, "fdSerialnumber", Integer.toString(j));
		            	}else
		            	{
		            		gridBlock.setCell(i, "fdSerialnumber", Integer.toString(++j));
		            	}
	            	}else
	            	{
	            		gridBlock.setCell(i, "fdSerialnumber", Integer.toString(j));
	            	}
	            	materialName = gridBlock.getCell(i, "fdMaterialName").toString();
	            	metric = gridBlock.getCell(i, "fdMetric").toString();
	            }
	        }
			outInfo = super.update(queryInfo, "ZMCMMaterialBasicInfoReport.update");
			
			return outInfo;
		}
		else {
			inInfo.setStatus(-1);
			inInfo.setMsg("没有选择物资！");
			return inInfo;
		}
	}
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: delete</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-03-07下午15:31:29  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCMMaterialBasicInfoReport.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}
	
	/**
	 * 检查是否添加重复的隐患项目存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query("ZMCMMaterialBasicInfoReport.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("选择的物资已经添加，请重新选择！");
			return true;
		}
		return false;
	}
}
